package com.zxp.zxpsmarthomeapk.communicate;

import android.os.AsyncTask;

import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;

/**
 * Created by zhangxianpeng on 2018/4/2.
 * 消息发送
 */

public class SendAsyncTask extends AsyncTask<String, Void, Void> {

    /**
     * 连接esp8266的IP和端口号
     * IP可通过指令在单片机开发板查到，端口豪可以自行设置，333为默认端口号
     */

    private static final String IP = "192.168.4.1";
    private static final int PORT = 333;
    private Socket client = null;
    private PrintStream out = null;

    @Override
    protected Void doInBackground(String... strings) {
        String string = strings[0];
        try {
            client = new Socket(IP, PORT);
            client.setSoTimeout(5000);
            //获取socket的输出流，用来发送数据到服务端
            out = new PrintStream(client.getOutputStream());
            out.print(string);
            out.flush();

            if (client == null) {
                return null;
            } else {
                out.close();
                client.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
